package com.cxs.service.impl;

import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.exception.HospitalException;
import com.cxs.mapper.MenuMapper;
import com.cxs.pojo.Menu;
import com.cxs.pojo.MenuExample;
import com.cxs.service.MenuService;
import com.cxs.utils.QueryUtil;
import com.cxs.vo.MenuTreeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/18  21:56
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Slf4j
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageInfo<Menu> menuListByPage(Integer id, String title, Integer pageNo, Integer pageSize) {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike(QueryUtil.createLikeResult(title));
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Menu> menuList = menuMapper.selectByExample(example);
        return new PageInfo<>(menuList);
    }

    @Override
    public List<MenuTreeVO> loadMenuMangerLeftTreeJson(String spread) {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(spread)) {
            try {
                int i = Integer.parseInt(spread);
                if(i == 1){
                    criteria.andParentIdIn(Arrays.asList(new Integer[]{0, 1}));
                }
            } catch (NumberFormatException e) {
                log.info("spead格式转换错误,spead={}",spread);
            }
        }

        // 查询所有权限菜单
        List<Menu> menuAllList = menuMapper.selectByExample(example);
        // 创建结果VO
        List<MenuTreeVO> treeVOList = new ArrayList<>();
        // 处理数据
        menuAllList.forEach(menu -> {
            MenuTreeVO treeVO = new MenuTreeVO();
            BeanUtils.copyProperties(menu, treeVO);
            // 封装是否展开
            if (menu.getSpread() == 1) {
                treeVO.setSpread(true);
            }
            treeVOList.add(treeVO);
        });

        return treeVOList;
    }

    @Override
    public void insert(Menu menu) {

        if (!StringUtils.isEmpty(menu.getId())) {
            menu.setId(null);
        }

        int result = menuMapper.insertSelective(menu);

        if (result != 1) {
            log.error("【添加菜单】:菜单添加失败 menu = {}", menu);
            throw new HospitalException(HospitalErrorResultEnum.MENU_SAVE_ERROR);
        }
    }

    @Override
    public void update(Menu menu) {
        int result = menuMapper.updateByPrimaryKeySelective(menu);

        if (result != 1) {
            log.error("【添加菜单】:菜单修改失败 menu = {}", menu);
            throw new HospitalException(HospitalErrorResultEnum.MENU_UPDATE_ERROR);
        }
    }

    @Override
    public Integer checkMenuHasChildren(Integer id) {

        Menu menu = menuMapper.selectByPrimaryKey(id);

        Integer result = -1;

        if (menu != null) {
            if (menu.getParentId() != 0 && menu.getParentId() != 1) {
                result = 0;
            } else {
                result = 1;
            }
        }
        return result;
    }

    @Override
    public void delete(Integer id) {
        int result = menuMapper.deleteByPrimaryKey(id);
        if (result != 1) {
            log.error("【菜单模块】:菜单删除失败 id = {}", id);
            throw new HospitalException(HospitalErrorResultEnum.MENU_DELETE_ERROR);
        }
    }
}
